package cn.b.sky.admin.security.context;

import cn.b.sky.admin.security.ManagementAuthentication;

public class ManagementContextImpl implements ManagementContext {

  private ManagementAuthentication authentication;

  @Override
  public ManagementAuthentication getAuthentication() {

    return authentication;
  }

  @Override
  public void setAuthentication(ManagementAuthentication authentication) {
    this.authentication = authentication;

  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof ManagementContextImpl) {
      ManagementContextImpl test = (ManagementContextImpl) obj;

      if ((this.getAuthentication() == null) && (test.getAuthentication() == null)) {
        return true;
      }

      if ((this.getAuthentication() != null) && (test.getAuthentication() != null)
          && this.getAuthentication().equals(test.getAuthentication())) {
        return true;
      }
    }

    return false;
  }

  @Override
  public int hashCode() {
    if (this.authentication == null) {
      return -1;
    } else {
      return this.authentication.hashCode();
    }
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(super.toString());

    if (this.authentication == null) {
      sb.append(": Null authentication");
    } else {
      sb.append(": Authentication: ").append(this.authentication);
    }

    return sb.toString();
  }
}
